<?php


abstract class DbTable
{
    public abstract function copy();

    public abstract function changeTo($obj);

    public static abstract function getTableName();

    public function checkRelationship()
    {
        return [];
    }

    public static abstract function getRequired();

    public abstract function loadData($data);

    public abstract function add();

    public abstract function delete();

    public abstract function edit($data);

    public abstract static function query($condition);

    protected static function checkRequired(array $data)
    {
        foreach (DbTable::getRequired() as $r) {
            if (isset($data[$r])) {
                continue;
            }
            return false;
        }
        return true;
    }

    public function __toString()
    {
        return json_encode($this);
    }

    public static function upperFirstName($attr,$first=false)
    {
        $names = explode("_", $attr);
        $name = "";
        for ($index = 0; $index < count($names); $index++) {
            if ($index == 0&&$first);
            else {
                $names[$index] = ucfirst($names[$index]);
            }

            $name = $name . $names[$index];
        }
        return $name;
    }

    public static function callSetMethod($obj, $attr, $data)
    {
        $name = "set" . self::upperFirstName($attr);
        call_user_func(array($obj, $name), $data);
    }

    public static function callGetMethod($obj, $attr)
    {
        $name = "get" . self::upperFirstName($attr);
        return call_user_func(array($obj, $name));
    }
}